import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Index from '@/views/Index'
import Cookies from 'js-cookie' // 导入js-cookie
import { Message } from 'element-ui';
// 将Message组件挂载到Vue原型上
Vue.prototype.$message = Message;

Vue.use(Router)

// 防止与当前路由相同时报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const router = new Router({
    routes: [
        {
            path: '/',
            name: 'index',
            component: Index,
            redirect: '/home',
            children: [
                {
                    path: '/home',
                    name: 'home',
                    component: () => import('@/views/home/index.vue')
                }, {
                    path: '/dataDirectory',
                    name: 'dataDirectory',
                    component: () => import('@/views/dataDirectory/index.vue')
                }, {
                    path: '/serviceDirectory',
                    name: 'serviceDirectory',
                    component: () => import('@/views/serviceDirectory/index.vue')
                }, {
                    path: '/theme',
                    name: 'theme',
                    component: () => import('@/views/theme/index.vue')
                }, {
                    path: '/seekResult',
                    name: 'seekResult',
                    component: () => import('@/views/seekResult/index.vue')
                }, {
                    path: '/openStatistics',
                    name: 'openStatistics',
                    component: () => import('@/views/openStatistics/index.vue')
                },
                {
                    path: '/hotspotData',
                    name: 'hotspotData',
                    component: () => import('@/views/openStatistics/hotspotData.vue')
                },
                {
                    path: '/interactiveCommunication',
                    name: 'interactiveCommunication',
                    component: () => import('@/views/interactiveCommunication/index.vue')
                },
              {
                path: '/newsInformation',
                name: 'newsInformation',
                component: () => import('@/views/newsInformation/index.vue')
              },
              {
                path:'/newsInformationDetails',
                name:'newsInformationDetails',
                component:()=>import('@/views/newsInformation/components/newsInformationDetails.vue')
              },
                {
                    path: '/personalCenter',
                    name: 'personalCenter',
                    component: () => import('@/views/personalCenter/index.vue')
                },
                {
                    path: '/test',
                    name: 'AboutView',
                    component: () => import('@/views/dataDirectory/AboutView.vue')
                },
                {
                    path: '/SelectAll',
                    name: 'SelectAll',
                    component: () => import('@/views/dataDirectory/SelectAll.vue')
                },
            ]
        },
      {
        path: '/logout',
        name: 'logout',
        component: () => import('@/views/logout.vue')
      },
    ]
})

router.beforeEach((to, from, next) => {
  const userInfo = Cookies.get('user'); // 获取用户信息
  if (!userInfo && to.path !== '/home' && from.path !== '/home') { // 如果未登录且不是跳转到首页
    // routeFlag = true
    next('/home'); // 强制跳转到首页
    // router.app.$message({
    //   message: '请登录2',
    //   type: 'warning'
    // });
    // setTimeout(()=>{

      // window.location.reload();
    // },1500)
  } else if (!userInfo && to.path !== '/home' && from.path === '/home') {
    router.go(0);
  } else {
    next(); // 直接跳转目标路由
  }
});


// router.beforeEach((to, from, next) => {
//   const userInfo = Cookies.get('userInfo'); // 获取用户信息
//   let apiUser = Cookies.get('api_user'); // 获取后台(图表)信息
//   // 添加额外的判断逻辑：若apiUser不存在但userInfo存在，则认为用户已退出登录
//   if (!apiUser && userInfo) {
//     // 清除userInfo，确保用户信息一致
//     Cookies.remove('userInfo');
//   }
//   if (!userInfo && to.path !== '/home') {   // 如果未登录且不是跳转到首页
//     // next('/home'); // 强制跳转到首页
//     router.app.$message({
//       message: '请登录',
//       type: 'warning'
//     });
//     next();
//     window.location.reload();
//   } else if (!userInfo && to.path === '/home') { // 如果未登录且跳转到首页
//     next(); // 直接跳转到首页
//   } else {
//     next(); // 直接跳转目标路由
//   }
// });

export default router



